package com.ct.goods.responsity.front;

import com.ct.goods.mapper.GoodsSeckillMapper;
import com.ct.goods.orm.GoodsSeckill;
import com.ct.repository.commons.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 秒杀商品仓库
 *
 * @author system
 */
@Repository
public class FrontGoodsSeckillRepository extends BaseServiceImpl<GoodsSeckillMapper, GoodsSeckill> {


    @Resource
    private GoodsSeckillMapper mapper;

    public List<GoodsSeckill> getListByGoodsIds(List<Long> goodsIds) {
        if (goodsIds == null || goodsIds.size() == 0) {
            return null;
        }
        return this.lambdaQuery()
                .in(GoodsSeckill::getGoodsId, goodsIds)
                .list();
    }


    public GoodsSeckill getByGoodsId(Long goodsId) {
        if (goodsId == null) {
            return null;
        }
        return mapper.getByGoodsId(goodsId);
        /*return this.lambdaQuery()
                .eq(GoodsSeckill::getGoodsId, goodsId)
                .last(" limit 1 ")
                .one();*/
    }



    /**
     * 修改秒杀商品库存和已售数量
     */
    public boolean decreStock(Long id,int stock,int soldNumber) {
        return this.lambdaUpdate()
                .set(GoodsSeckill::getStock,stock)
                .set(GoodsSeckill::getSoldNumber,soldNumber)
                .eq(GoodsSeckill::getId,id)
                .update();
    }

    /**
     * 修改秒杀商品库存和已售数量
     */
    public boolean decreStock(Long id,int version) {
        return mapper.decreStock(id,version);
    }



    /**
     * 修改秒杀商品库存和已售数量
     */
    public boolean decreStock(Long id,int oldVersion,int newVersion,int stock,int soldNumber) {
        return this.lambdaUpdate()
                .set(GoodsSeckill::getVersion,newVersion)
                .set(GoodsSeckill::getStock,stock)
                .set(GoodsSeckill::getSoldNumber,soldNumber)
                .eq(GoodsSeckill::getId,id)
                .eq(GoodsSeckill::getVersion,oldVersion)
                .update();
    }

}
